package com.daman.goods

import com.daman.goods.CategoryOps._
import com.daman.{Respond, AppAction}
import com.daman.db.{DBToolkit, DbOps}
import xitrum.SkipCsrfCheck
import xitrum.annotation.{POST, PUT, GET, Swagger}


@GET("p/goods/:id<[0-9]+>")
@Swagger(
  Swagger.Note("get the goods detail of one goods id is goods_id")
)
class Goods extends GoodsApi{
  override def execute(): Unit = {
    val id      =  param[Int]("id")
    val result =  GoodsOps.findById(id)
    respondOK(result)
  }
}

@GET("p/:id<[0-9]+>/goods/")
@Swagger(
  Swagger.Note("get the goods list of one category")
)
class GoodsOfCategory extends GoodsApi{
  override def execute(): Unit = {
    val id      =  param[Int]("id")
    val result =  GoodsOps.getGoodsOfCategory(id)
    respondOK(result)
  }
}

@POST("p/goods")
@Swagger(
  Swagger.Note("post goods to database")
)
class GoodsPost extends GoodsApi{
  override def execute(): Unit = {
    getPost((data:Map[String,String]) =>{
      val result = GoodsOps.insert(data)
      respondOK(result)
    })
  }
}

@PUT("p/goods/:id<[0-9]+>")
@Swagger(
  Swagger.Note("update goods by  goods_id")
)
class GoodsPut extends GoodsApi{
  override def execute(): Unit = {
    val id      =  param[Int]("id")
    getPost((data:Map[String,String])=>{
      val result = GoodsOps.updById(id,data)
      respondOK(result)
    })
  }
}


object  GoodsOps extends  DbOps{
  override def dbName: String = GoodsTable.tableName

  override  def insert(fields:Map[String,String]) = {
    val conn = DBToolkit.getConn()
    val safeFields = GoodsTable.created_at
    val safeValues = "NOW()"
    val result = DBToolkit.insertReturnId(conn,dbName,fields,safeFields,safeValues)
    DBToolkit.closeConn(conn)
    result
  }

  override def updById(id:Int,fields:Map[String,String]) = {
    val conn = DBToolkit.getConn()
    val safeFieldValues =  GoodsTable.updated_at +"= NOW()"
    val result = DBToolkit.update(conn,dbName,fields,Map(m_id->id.toString),safeFieldValues)
    DBToolkit.closeConn(conn)
    result
  }

  def getGoodsOfCategory(id:Int) ={
    val conn = DBToolkit.getConn()
    val sql = "SELECT * FROM "+GoodsTable.tableName+" WHERE "+GoodsTable.category_id+"=?"
    DBToolkit.readMap(conn,sql,List(id.toString))
  }
}

@Swagger(
  Swagger.Resource("goods", "Operations of goods list insert update and ... "),
  Swagger.Produces("application/json")
)
trait  GoodsApi extends  AppAction  with SkipCsrfCheck {
}

object  GoodsTable{
  val tableName = "da_goods"
  val id = "id"
  val name ="name"
  val category_id = "category_id"
  val description = "description"
  val meta_keywords = "meta_Keywords"
  val meta_description = "meta_description"
  val short_description = "short_description"
  val status = "status"
  val available_start = "available_start"
  val available_end = "available_end"
  val created_at = "created_at"
  val updated_at = "updated_at"
  val deleted_at = "deleted_at"

  object STATUSVALUE {
    val CREATED = "CREATED"
    val ONSALE = "ONSALE"
    val DOWNSALE ="DOWNSALE"
    val DELETE = "DELETE"
  }
}